<?php

namespace App\Controller\Home;

use App\Controller\Example\CartExample;
use App\Controller\Example\CartGoodsExample;
use App\Controller\Example\UserAddrExample;
use App\Controller\Example\UserExample;
use App\Models\BarsGoodsModel;
use App\Models\CartGoodsModel;
use App\Models\CartModel;
use App\Models\ConfigModel;
use App\Models\Conn;
use App\Models\UsersAddrModel;
use App\Models\UsersModel;
use Config\Config;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;

class Order
{
    /**
     * 订单列表
     * entry  token status
     * return json
     * */
    public function orderList($data){
        $assemble = Common::assemble(new CartExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn,$assemble->token)["id"];
        $assemble->status = $assemble->status ? :"1,2,3,4,5,6";
        $cartInsideId = CartModel::getCartInsideUid($conn, $assemble,$identical);
        $userAddrExample = new UserAddrExample();
        $userAddrExample->uid = $assemble->uid;
        $cartGoodsExample = new CartGoodsExample();
        $cartGoodsExample->uid = $assemble->uid;
        foreach ($cartInsideId as $item => $value){
            $userAddrExample->id = $value["addrid"];
            $cartInsideId[$item]["addr"] = UsersAddrModel::getUserAddr($conn,$userAddrExample);
            $cartGoodsExample->cid = $value["id"];
            $orderCartGoodsInsideCid = CartGoodsModel::getOrderCartGoodsInsideCid($conn, $cartGoodsExample);
            foreach ($orderCartGoodsInsideCid as $key => $val){
                $orderCartGoodsInsideCid[$key]["img"] = BarsGoodsModel::getGoodsSingle($conn,$val["gid"])["img"];
                $orderCartGoodsInsideCid[$key]["sku"] = BarsGoodsModel::getGoodsSku($conn,$val["gid"],$val["sku_id"]);
            }
            $cartInsideId[$item]["goods"] = $orderCartGoodsInsideCid;
        }
        $res["list"] = $cartInsideId;
        $res["count"] = CartModel::getCartInsideUidCount($conn,$assemble);
        Result::success($res);
    }
    /**
     * 确认订单、待支付
     * entry  token id,addrid,remarks
     * return json
     * */
    public function confirmOrder($data){
        $assemble = Common::assemble(new CartExample(), $data);
        if($assemble->addrid ==null){
            Result::error(ErrorEnum::DELIVERYADDRESSDOESNOTEXIST);
        }
        if($assemble->addrid ==null){
            Result::error(ErrorEnum::DELIVERYADDRESSDOESNOTEXIST);
        }
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        if(empty(UsersModel::getUserInside($conn, $assemble->token)["id_card"])){
            //Result::error(ErrorEnum::PLEASECOMPLETEREALNAMEAUTHENTICATIONFIRST);
        }
        $cartInsideId = CartModel::getCartInsideId($conn, $assemble);
        if($cartInsideId["status"] !=1){
            Result::error(ErrorEnum::INVALIDORDER);
        }
        $conn->shiwuStart();
        $assemble->status = 2;
        $assemble->stime = "NOW()";
        if(CartModel::editCartInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 删除订单
     * entry  token id
     * return json
     * */
    public function delOrder($data){
        $assemble = Common::assemble(new CartExample(), $data);
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $cartInsideId = CartModel::getCartInsideId($conn, $assemble);
        if($cartInsideId["status"] == -1){
            Result::error(ErrorEnum::DELETIONSUCCEEDED);
        }
        $conn->shiwuStart();
        $assemble->status = -1;
        if(CartModel::editCartInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::DELETEFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 确认收货
     * entry  token id
     * return json
     * */
    public function confirmReceipt($data){
        $assemble = Common::assemble(new CartExample(), $data);
        $conn = new Conn();
        $UserExample = Common::assemble(new UserExample(),UsersModel::getUserInside($conn, $assemble->token));
        $assemble->uid = $UserExample->id;
        $cartInsideId = CartModel::getCartInsideId($conn, $assemble);
        if($cartInsideId["status"] != 4){
            Result::error(ErrorEnum::INVALIDORDER);
        }
        $conn->shiwuStart();
        $assemble->status = 5;
        if(CartModel::editCartInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SETTINGFAILED);
        }
        if($cartInsideId["type"] == 2){
            $orderMoney = $cartInsideId["pay_price"];
            $CartExample = Common::assemble(new CartExample(), $cartInsideId);
            Sett::addUserAch($conn, $UserExample, $orderMoney,$CartExample);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 退货
     * entry  token id
     * return json
     * */
    public function returnGoods($data){
        $assemble = Common::assemble(new CartExample(), $data);
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $cartInsideId = CartModel::getCartInsideId($conn, $assemble);
        if($cartInsideId["status"] != 4 && $cartInsideId["type"] != 2){
            Result::error(ErrorEnum::INVALIDORDER);
        }
        $conn->shiwuStart();
        $assemble->status = 6;
        if(CartModel::editCartInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SETTINGFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 查看退货地址
     * entry  token id
     * return json
     * */
    public function getReturnAddr(){
        $conn = new Conn();
        $configSingleValName = ConfigModel::getConfigSingleValName($conn, Config::RETURN_ADDR);
        Result::success($configSingleValName);
    }
    /**
     * 查看体验自动收货时间
     * entry  token id
     * return json
     * */
    public function getExperienceTime(){
        $conn = new Conn();
        $configSingleValName = ConfigModel::getConfigSingleValName($conn, Config::AUTO_RECEIVING);
        Result::success($configSingleValName);
    }
}